/** 
 * Copyright (C), 2010-2012, TIETIAO Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:LabelServiceImpl.java
 * Version:1.0
 * Date:2011-12-8
 * Description: 标签接口实现
 * author:zhijian.zhang
 */
package com.tietiao.service.label;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tietiao.common.util.PageModel;
import com.tietiao.common.util.PropertyUtil;
import com.tietiao.common.util.StringUtil;
import com.tietiao.dao.LableMapper;
import com.tietiao.dao.model.Lable;
import com.tietiao.dao.model.LableCriteria;

@Service
public class LabelServiceImpl implements LabelService {

	@Resource
	private LableMapper lableMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tietiao.service.label.LabelService#addLabels(int,
	 * java.lang.String)
	 */
	@Override
	public String addLabels(int userId, String labels) {

		if (StringUtil.checkStrIsNotNull(labels)) {
			// 找到已经存在的Lable列表
			String[] labelArr = labels.split(" ");
			LableCriteria lableCriteria = new LableCriteria();
			LableCriteria.Criteria criteria = lableCriteria.createCriteria();
			criteria.andUserIdEqualTo(userId);
			List<String> oldLabArr = new ArrayList<String>();
			List<String> labArr = new ArrayList<String>();
			for (String tempLabel : labelArr) {
				labArr.add(tempLabel);
				oldLabArr.add(tempLabel);
			}
			criteria.andLabelNameIn(labArr);
			List<Lable> exLables = lableMapper.selectByExample(lableCriteria);
			// 排除已存在的标签
			if (exLables != null && exLables.size() > 0) {
				for (Lable exLable : exLables) {
					labArr.remove(exLable.getLabelName());
				}
			}
			List<Lable> insertLables = new ArrayList<Lable>();
			if (labArr.size() > 0) {
				for (String lableName : labArr) {
					Lable lable = new Lable();
					lable.setLabelName(lableName);
					lable.setUserId(userId);
					lable.setAttentionNum(0);
					lable.setCommodityNum(0);
					lable.setCommodityTietiaoNum(0);
					insertLables.add(lable);
				}
				lableMapper.batchInsert(insertLables);
			}
			lableCriteria.clear();
			criteria = lableCriteria.createCriteria();
			criteria.andUserIdEqualTo(userId);
			criteria.andLabelNameIn(oldLabArr);
			List<Lable> userLables = lableMapper.selectByExample(lableCriteria);
			StringBuilder lableIds = new StringBuilder();
			for (int i = 0, len = userLables.size(); i < len; i++) {
				lableIds.append(userLables.get(i).getLabelId());
				if (i < len - 1) {
					lableIds.append(",");
				}
			}
			return lableIds.toString();
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.label.LabelService#updateLabelCommodityNums(java.
	 * lang.String)
	 */
	@Override
	public void addOrSubOneLabelCommodityNums(String labelIds, boolean addFlag) {
		lableMapper.batchUpdateNumsByPrimaryKeysSelective(addFlag, null, null,
				labelIds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.label.LabelService#updateAttenOrNoAttenNumsLabels
	 * (java.lang.String, boolean)
	 */
	@Override
	public void addOrSubOneAttenNums(String labelIds,
			boolean attendFlag) {
		lableMapper.batchUpdateNumsByPrimaryKeysSelective(null, null,
				attendFlag, labelIds);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.label.LabelService#updateTietiaoNumsLabels(java.lang
	 * .String, boolean)
	 */
	@Override
	public void addOrSubOneTietiaoNum(String commodityIds, boolean tietiaoFlag) {
		lableMapper.batchUpdateNumsByPrimaryKeysSelective(null, tietiaoFlag,
				null, commodityIds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.label.LabelService#getLablePageModels(com.tietiao
	 * .dao.model.Lable, java.lang.String, int, int)
	 */
	@Override
	public PageModel<Lable> getLablePageModels(Lable lable, String orderClause,
			int pageNo, int pageSize) {
		LableCriteria criteria = new LableCriteria();
		if (lable != null) {
			criteria = getLableCriteria(lable);
		}
		int start = (pageNo - 1) * pageSize;
		criteria.setStart(start);
		criteria.setLimit(pageSize);
		if (StringUtil.checkStrIsNotNull(orderClause)) {
			criteria.setOrderByClause(orderClause);
		}
		PageModel<Lable> pageModel = new PageModel<Lable>(
				lableMapper.selectByExample(criteria), pageNo, pageSize,
				lableMapper.countByExample(criteria));
		return pageModel;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.label.LabelService#getLables(com.tietiao.dao.model
	 * .Lable, java.lang.String)
	 */
	@Override
	public List<Lable> getLables(Lable lable, String orderClause) {
		LableCriteria criteria = new LableCriteria();
		if (lable != null) {
			criteria = getLableCriteria(lable);
		}
		if (StringUtil.checkStrIsNotNull(orderClause)) {
			criteria.setOrderByClause(orderClause);
		}
		return lableMapper.selectByExample(criteria);
	}
	
	

	/* (non-Javadoc)
	 * @see com.tietiao.service.label.LabelService#getPersionCommonLables(int)
	 */
	@Override
	public List<String> getPersionCommonLables(int userId) {
		
		LableCriteria lableCriteria = new LableCriteria();
		LableCriteria.Criteria criteria = lableCriteria.createCriteria();
		criteria.andUserIdEqualTo(userId);
		List<Lable> lables = lableMapper.selectByExample(lableCriteria);
		
		List<String> result = new ArrayList<String>();
		Map<String,String> distinctMap = new HashMap<String, String>();
		if(lables != null) 
		{
			for (Lable lable : lables) {
				result.add(lable.getLabelName());
				distinctMap.put(lable.getLabelName(), "1");
			}
		}
	
		return result;
	}
	/**
	 * 
	 * 功能：封装查询条件(只供getLables、getLablePageModels使用)
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-9
	 * 
	 * @return
	 */
	private LableCriteria getLableCriteria(Lable lable) {
		LableCriteria lableCriteria = new LableCriteria();
		LableCriteria.Criteria criteria = lableCriteria.createCriteria();
		if (lable.getUserId() != null) {
			criteria.andUserIdEqualTo(lable.getUserId());
		}
		if (lable.getAttentionNum() != null) {
			criteria.andAttentionNumGreaterThanOrEqualTo(lable
					.getAttentionNum());
		}
		if (lable.getCommodityNum() != null) {
			criteria.andCommodityNumGreaterThanOrEqualTo(lable
					.getCommodityNum());
		}
		if (lable.getCommodityTietiaoNum() != null) {
			criteria.andCommodityTietiaoNumGreaterThanOrEqualTo(lable
					.getCommodityTietiaoNum());
		}
		if (StringUtil.checkStrIsNotNull(lable.getLabelName())) {
			criteria.andLabelNameLike(lable.getLabelName());
		}
		return lableCriteria;
	}

	/* (non-Javadoc)
	 * @see com.tietiao.service.label.LabelService#getLabelInfo()
	 */
	@Override
	public List<Lable> getLabelInfo() {
		return lableMapper.getLableInfo();
	}
}
